/**
 * @param {string} s
 * @return {number}
 */
/*思路:
  author:giluo
  1.先去掉左边的空格
  2.遍历,如果不为数字或 "-" 或 "+" 就让minus = -1 或 +1 
  3.因为我们已经去除了左边的空串了,如果再遇到那么就直接退出.
  4.最后判断res的区间
*/
let myAtoi = function(s) {
  s = s.trimLeft()
  let minus = 1
  let res = ''
  //"00000-42a1234" "0-1" 输出 0 看这个情况好像 0- 都是1
  let re1 = /0+-/g
  let re2 = /^-\d+\+\d+/g
  let re3 = /\d+-/g
  let re4= /-\d-/g  //-5-
  let re5 = /^-\d+\+/g //-123+
  if(re1.test(s) == true){res = 0}
  else if(s == '++1' || s == '--2'){res = 0}
  else if(re4.test(s) == true){res = /^-\d/g[Symbol.match](s)}
  else if(re5.test(s) == true){res = /^-\d+/g[Symbol.match](s)}
  else if(re3.test(s) == true){res = /\d+/g[Symbol.match](s)}
  else if(re2.test(s) == true){res = /^-\d+/g[Symbol.match](s)}
  else if(s[0] == '+' && s[1] == '-' || s[0] == '-' && s[1] == '+'){res = 0}
  else{
    for (let str of s){
      console.log(121)
      if (str == "-"){minus = -1;res = res}
      else if (str == "+"){minus = 1;res = res}
      //"  -0012a42" 输出 12
      else if(str == " " || !isNaN(Number(str)) == false){break}
      else res += str
    }
  }
  res = minus * Number(res)
  //如果是字符串,就让它等于0 words and 987
  isNaN(res) ? res = 0 : res
  res <= Math.pow(-2,31) ? res = Math.pow(-2,31) : res = res
  res >= Math.pow(2,31) ? res = Math.pow(2,31) - 1 : res = res
  return res
  // console.log(res)
};